package com.liuqi.config;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * Kafka通知监听
 *
 * @author  LiuQi 2025/3/30-10:17
 * @version V1.0
 **/
@Component
public class NoticeKafkaListener {
    @Autowired
    private Set<NoticeProcessor> processors;

    /**
     * 监听数据更新通知，刷新缓存
     * 此处不能指定分组，而是使用KafkaConfiguration里面的随机分组；如果指定的话会导致多节点时部分节点接收不到消息；
     * 但另外一个问题仍旧存在：在所有节点部署到一起时，data-service中的将接收不到消息如通知
     */
    @KafkaListener(topics = "${iot.kafka.topic.notice}")
    public void processNotice(ConsumerRecord<String, String> record) {
        if (null == processors) {
            return;
        }

        processors.forEach(processor -> processor.process(record));
    }
}
